package com.cusc.lib_api;

import android.app.Application;
import android.content.Context;

import com.cusc.lib_api.config.API;
import com.cusc.lib_api.http.OkHeaderInterceptor;
import com.cusc.lib_api.services.DepartmentServices;
import com.cusc.lib_api.services.DictServices;
import com.cusc.lib_api.services.LoginServices;
import com.cusc.lib_api.utils.SSLContextUtil;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/18.
 * 包名: com.cusc.lib_api
 * 类名: OSAndroidMvvm
 */
public class RetrofitManager {
    public static RetrofitManager retrofitManager;
    public static Context mContext;
    private Retrofit mRetrofit;
    OkHttpClient.Builder okHttpBuilder;

    private RetrofitManager() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        OkHeaderInterceptor headering = new OkHeaderInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpBuilder = new OkHttpClient.Builder();
        okHttpBuilder.interceptors().add(logging);
        okHttpBuilder.interceptors().add(headering);

        SSLContext sslContext = SSLContextUtil.getDefaultSLLContext();
        if (sslContext != null) {
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            okHttpBuilder.sslSocketFactory(socketFactory);
        }
        okHttpBuilder.hostnameVerifier(SSLContextUtil.HOSTNAME_VERIFIER);
        mRetrofit = new Retrofit.Builder().client(okHttpBuilder.build())
                .baseUrl(API.URL_HOST_USER).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build();
    }

    public static void init(Application application) {
        mContext = application;
    }

    public static RetrofitManager getInstance() {
        if (retrofitManager == null) {
            synchronized (RetrofitManager.class) {
                if (retrofitManager == null) {
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }


    public void addToken(final String token) {
        if (okHttpBuilder != null)
            okHttpBuilder.addInterceptor(chain -> {
                Request original = chain.request();
                Request.Builder requestBuilder = original.newBuilder()
                        .header("Authorization", "Bearer " + token);
                Request request = requestBuilder.build();
                return chain.proceed(request);
            });
    }

    /**
     * 创建登陆模块服务内容
     * @return
     */
    public LoginServices getLoginService() {
        return mRetrofit.create(LoginServices.class);
    }

    public DepartmentServices getDepartmentService(){
        return mRetrofit.create(DepartmentServices.class);
    }

    public DictServices getDicServices(){
        return mRetrofit.create(DictServices.class);
    }
}
